package com.hframe.other;

import com.hframe.utils.FrameUtils;

import javax.servlet.http.HttpSession;


/**
 * 重复提交验证
 * 
 * @author heqian
 *
 */
public class RepeatVerify {

	/**
	 * 获得一个唯一的Token
	 * 
	 * @param key
	 * @return
	 */
	public static String getToken(HttpSession session, String key) {
		String token = (String) session.getAttribute("RepeatVerify" + key);
		String code = FrameUtils.create_random_str(key);
		while (code.equals(token)) {
			code = FrameUtils.create_random_str(key);
		}
		session.setAttribute("RepeatVerify" + key, code);
		return code;
	}

	/**
	 * 验证重复
	 * 
	 * @param key
	 * @param token
	 * @return
	 */
	public static boolean isVerify(HttpSession session, String key, String token) {
		return null != token && token.equals(session.getAttribute("RepeatVerify" + key));
	}

	/**
	 * 删除一个KEY
	 * 
	 * @param key
	 * @return
	 */
	public static void remove(HttpSession session, String key) {
		session.removeAttribute("RepeatVerify" + key);
	}
}
